//
//  ViewController.m
//  WangI
//
//  Created by wjn on 15/11/23.
//  Copyright © 2015年 WJN. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, NewsCellDelegate, EntertainmentDetailPageDelegate, HotPointDelegate>
@property (nonatomic, retain)NSMutableArray *newsArray;
@property (nonatomic, retain)NSMutableArray *entertainmentArray;
@property (nonatomic, retain)NSMutableArray *hotArray;
@property (nonatomic, retain)NSMutableArray *sportsArray;
@property (nonatomic, retain)NSMutableArray *selfCityArray;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)NSMutableArray *buttonArray;
@property (nonatomic, retain)UIActivityIndicatorView *indicator;

@end

@implementation ViewController


- (void)dealloc {
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"网易新闻";
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    self.navigationController.navigationBar.translucent = NO;
    //请求数据
    [self getNewsNetData];
    [self getEntertainmentNetData];
    [self getHotNetData];
    [self getSportsNetData];
    [self getSelfCityNetData];
    
    [self showIndicator];
    
}

#pragma mark -- 详情页面协议实现方法


- (void)showDetailPage:(NSString *)idsURL withType:(NSString *)type {
    NewsDetailViewController *detailVC = [[NewsDetailViewController alloc] init];
    detailVC.ids = idsURL;
    detailVC.type = type;
    [self.navigationController pushViewController:detailVC animated:YES];
    
    
}

#pragma mark -- CollectionView滑动时的方法

//添加菊花转

- (void)showIndicator {
    _indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    _indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
    [_indicator setCenter:self.view.center];
    _indicator.backgroundColor = [UIColor blackColor];
    _indicator.layer.cornerRadius = 6;
    _indicator.layer.masksToBounds = YES;
    _indicator.color = [UIColor grayColor];
    [_indicator startAnimating];
    [self.collectionView addSubview:_indicator];
    [_collectionView release];
}



//减速触发

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat distance = scrollView.contentOffset.x;
    for (int i = 0; i < self.buttonArray.count; i++) {
        if (distance == i * self.view.frame.size.width) {
            UIButton *button = self.buttonArray[i];
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:19];
        } else {
        UIButton *button = self.buttonArray[i];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
    }
}

- (void)getNewsNetData {
    [SANetWorkingTask request:kNewsGETURL parmater:nil block:^(id result) {
        NSDictionary *dic = result;
        NSArray *array = [dic valueForKey:@"T1348647909107"];
        for (NSDictionary *dic in array) {
            NewSecModel *secModel = [[NewSecModel alloc] initWithDicitionary:dic];
            [self.newsArray addObject:secModel];
        }
        [_collectionView reloadData];
    }];
}

- (void)getEntertainmentNetData {
    [SANetWorkingTask request:kEntertanmentGETURL parmater:nil block:^(id result) {
        NSDictionary *dic = result;
        NSArray *arr = [dic valueForKey:@"T1348648517839"];
        for (int i = 0; i < arr.count; i++) {
            EntertainmentModel *entertainmentModel = [[EntertainmentModel alloc] initWithDicitionary:arr[i]];
            [self.entertainmentArray addObject:entertainmentModel];
        }
        [_collectionView reloadData];
    }];
}

- (void)getHotNetData {
    [SANetWorkingTask request:kHotGETUrl parmater:nil block:^(id result) {
        NSDictionary *dic = result;
        NSArray *arr = [dic valueForKey:@"推荐"];
        for (int i = 0; i < arr.count; i++) {
            HotModel *hotModel = [[HotModel alloc] initWithDicitionary:arr[i]];
            [self.hotArray addObject:hotModel];
        }
        [_collectionView reloadData];
    }];
}

- (void)getSportsNetData {
    [SANetWorkingTask request:kSportsGETURL parmater:nil block:^(id result) {
        NSDictionary *dic = result;
        NSArray *arr = [dic valueForKey:@"T1348649079062"];
        for (NSDictionary *dic in arr) {
            SportsModel *sportsModel = [[SportsModel alloc] initWithDicitionary:dic];
            [self.sportsArray addObject:sportsModel];
        }
         [_collectionView reloadData];
    }];
}

- (void)getSelfCityNetData {
    [SANetWorkingTask request:kSelfCityGETURL parmater:nil block:^(id result) {
        
        NSDictionary *dic = result;
        NSArray *arr = [dic valueForKey:@"大连"];
        for (NSDictionary *dic in arr) {
            HotModel *hotModel = [[HotModel alloc] initWithDicitionary:dic];
            hotModel.ids = hotModel.docid;
            [self.selfCityArray addObject:hotModel];
        }
        [_collectionView reloadData];
        [_indicator stopAnimating];
    }];
}

- (UIScrollView *)scrollView {
    NSArray *array = [[NSArray alloc] initWithObjects:@"头条", @"娱乐", @"热点", @"体育", @"本地", nil];
    if (!_scrollView) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)];
        _scrollView.backgroundColor = [UIColor whiteColor];
        for (int i = 0; i < array.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [button setTitle:array[i] forState:UIControlStateNormal];
            button.tintColor = [UIColor blackColor];
            [self.buttonArray addObject:button];
            button.tag = 100 + i;
            button.titleLabel.textAlignment = NSTextAlignmentCenter;
            button.frame = CGRectMake(5 + i * 80, 0, 40, _scrollView.frame.size.height);
            [_scrollView addSubview:button];
            button.backgroundColor = [UIColor whiteColor];
            [button addTarget:self action:@selector(buttonDidPress:) forControlEvents:UIControlEventTouchUpInside];
        }
        UIButton *button = self.buttonArray[0];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:19];
    }
    return _scrollView;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
        layout.itemSize = CGSizeMake(self.view.frame.size.width , self.view.bounds.size.height - 108);
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.pagingEnabled = YES;
        [_collectionView registerClass:[ScrollCollectionViewCell class] forCellWithReuseIdentifier:@"ScrollCollectionViewCellIdentifier"];
        [_collectionView registerClass:[EntertainmentCollectionViewCell class] forCellWithReuseIdentifier:@"EntertainmentCollectionViewCellIdentifier"];
        [_collectionView registerClass:[HotCollectionViewCell class] forCellWithReuseIdentifier:@"HotCollectionViewCellIdentifier"];
        [_collectionView registerClass:[EntertainmentCollectionViewCell class] forCellWithReuseIdentifier:@"SportIdentifier"];
        [_collectionView registerClass:[HotCollectionViewCell class] forCellWithReuseIdentifier:@"SelfCityIdentifier"];
    }
    return _collectionView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 5;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item == 0) {
        ScrollCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ScrollCollectionViewCellIdentifier" forIndexPath:indexPath];
        cell.delegate = self;
        if (self.newsArray.count) {
            cell.newsArray = self.newsArray;
        }
        return cell;
    } else if (indexPath.item == 1) {
        EntertainmentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EntertainmentCollectionViewCellIdentifier" forIndexPath:indexPath];
        cell.delegate = self;
        if (self.entertainmentArray.count) {
            cell.entertainmentArray = self.entertainmentArray;
        }
        return cell;
    } else if (indexPath.item == 2) {
        HotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotCollectionViewCellIdentifier" forIndexPath:indexPath];
        cell.delegate = self;
        if (self.hotArray.count) {
            cell.hotArray = self.hotArray;
        }
        return cell;
    }
        else if (indexPath.item == 4) {
        HotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SelfCityIdentifier" forIndexPath:indexPath];
            cell.delegate = self;
        if (self.selfCityArray) {
            cell.hotArray = self.selfCityArray;
        }
        return cell;
    }
    else {
        EntertainmentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SportIdentifier" forIndexPath:indexPath];
        cell.delegate = self;
        if (self.sportsArray.count) {
            cell.entertainmentArray = self.sportsArray;
        }
        return cell;
    }
}

#pragma -- mark button点击方法

- (void)buttonDidPress:(UIButton *)sender {
    self.collectionView.contentOffset = CGPointMake((sender.tag - 100) * self.view.frame.size.width, 0);
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    sender.titleLabel.font = [UIFont systemFontOfSize:19];
    for (int i = 0; i < self.buttonArray.count; i++) {
        if ([self.buttonArray[i] tag] != sender.tag) {
            UIButton *button = self.buttonArray[i];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
    }
}

#pragma -- mark Lazyload

- (NSMutableArray *)newsArray {
    if (!_newsArray) {
        self.newsArray = [[NSMutableArray alloc] init];
    }
    return _newsArray;
}

- (NSMutableArray *)hotArray {
    if (!_hotArray) {
        self.hotArray = [[NSMutableArray alloc] init];
    }
    return _hotArray;
}

- (NSMutableArray *)sportsArray {
    if (!_sportsArray) {
        self.sportsArray = [[NSMutableArray alloc] init];
    }
    return _sportsArray;
}

- (NSMutableArray *)entertainmentArray {
    if (!_entertainmentArray) {
        self.entertainmentArray = [[NSMutableArray alloc] init];
    }
    return _entertainmentArray;
}

- (NSMutableArray *)selfCityArray {
    if (!_selfCityArray) {
        self.selfCityArray = [[NSMutableArray alloc] init];
    }
    return _selfCityArray;
}

- (NSMutableArray *)buttonArray {
    if (!_buttonArray) {
        self.buttonArray = [[NSMutableArray alloc] init];
    }
    return _buttonArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
